using UnityEngine;
using System.Collections;

public class Threading
{
	public bool Execute = true;
	public Vector3 player; 
	public int value = 1;

	System.Collections.Generic.Queue<float> values;
	System.Threading.Thread t1; 
	private static int currentFrame = 0;
	private static int number = 1;
	
	private static Threading _instance;
    private static object _lock = new object();
	static Threading() {}
	
    public static Threading Instance // singleton makes thread safe
    {
        get
        {
            if (_instance == null)
            {
                lock(_lock)
                {
                    if (_instance == null) _instance = new Threading();
                }
            }
            return _instance;
        }
    }
	 private Threading() {}

	void Start () 
	{
		
        t1 = new System.Threading.Thread(executeThread);
        t1.Start();
	}

    void executeThread()
    {
        while (Execute == true)
        {
            System.Threading.Thread.Sleep(500);
            values.Enqueue(player.x);
            UnityEngine.Debug.Log("value");
        }
		
		System.IO.StreamWriter write = new System.IO.StreamWriter("values.txt"); // writes to file every 5 seconds
		
		while (values.Count > 0)
        {
            write.WriteLine(values.Dequeue().ToString());
        }
        write.Close();
    }
	
	// Update is called once per frame
	void Update () 
	{
	
		currentFrame = currentFrame + 10;
        if (Time.frameCount == currentFrame)
		{
			values.Enqueue(player.x);
			number++;
			values.Enqueue(number);
		}
	}

    void OnDestroy()
    {
		Execute = false;
        t1.Abort();
    }
	
	 void OnCollisionEnter(Collision collision) 
	{
       values.Enqueue(player.x);
    }
	
	public void ObjectReport() // allows non Mono Behavior to report to log
	{
		currentFrame = currentFrame + 10;
        if (Time.frameCount == currentFrame)
		{
	   		values.Enqueue(player.x);
	   		values.Enqueue(player.y);
	   		values.Enqueue(player.z);
		}
	}
	
}


